package com.fastwire.carriere.service;

import java.util.List;

import javax.jws.WebService;

import com.fastwire.carriere.common.util.CommonLogger;
import com.fastwire.carriere.common.util.ResourceUtil;
import com.fastwire.carriere.controller.ControllerProxy;
import com.fastwire.carriere.rad.controller.RadProcessController;
import com.fastwire.carriere.rad.controller.RadProcessControllerImp;
import com.fastwire.carriere.rad.controller.TrapMsgController;
import com.fastwire.carriere.rad.controller.TrapMsgControllerImp;
import com.fastwire.carriere.rad.entityVo.RadAlarmVo;
import com.fastwire.carriere.rad.entityVo.RadLogVo;
import com.fastwire.carriere.rad.snmpadapter.TrapReceiver;
import com.fastwire.carriere.rad.util.NetworkUtil;
import com.fastwire.carriere.service.webui.exception.ServiceException;
import com.fastwire.carriere.user.vo.SessionVo;

/*
 * Creted on 20120501
 * 
 * @author: Lei Jun
 */
@WebService(endpointInterface = "com.fastwire.carriere.service.RadService")
public class RadServiceImpl implements RadService
{
    private static TrapMsgController trapCtrl = new TrapMsgControllerImp();
    static 
    {
        TrapReceiver.startListen(trapCtrl);
    }
    
    @Override
    public String fullDiscovery(List<String> ipSubnetList, SessionVo sessionVo) throws ServiceException
    {
        try
        {
            RadProcessController ctrl = (RadProcessController) ResourceUtil
                    .getSessionBindClassByName(RadProcessControllerImp.class.getName(), sessionVo,
                            false);
            ctrl.fullDiscovery(ipSubnetList);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            
            String ex = ControllerProxy.getRootException(e);
            throw new ServiceException(ex);
        }

        return null;
    }

    @Override
    public String adhocDiscovery(String ip, SessionVo sessionVo) throws ServiceException
    {
        try
        {
            if(!NetworkUtil.ipv4Check(ip))
            {
                return "IP is not correct.";
            }
            RadProcessController ctrl = (RadProcessController) ResourceUtil
                    .getSessionBindClassByName(RadProcessControllerImp.class.getName(), 
                                               sessionVo, true);
            ctrl.adhocDiscovery(ip);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            
            String ex = ControllerProxy.getRootException(e);
            throw new ServiceException(ex);
        }

        return null;
    }

    @Override
    public String loopback(String ip, String testType, SessionVo sessionVo) throws ServiceException
    {
        String rlt = null;
        try
        {
            if(!NetworkUtil.ipv4Check(ip))
            {
                return "IP is not correct.";
            }
            // testType is reserved for future use

            RadProcessController ctrl = (RadProcessController) ResourceUtil
                    .getSessionBindClassByName(RadProcessControllerImp.class.getName(),
                            sessionVo,
                            false);
            rlt = ctrl.loopback(ip, testType);
        }
        catch (Exception e)
        {
            e.printStackTrace();

            String ex = ControllerProxy.getRootException(e);
            throw new ServiceException(ex);
        }

        return rlt;
    }

    /* (non-Javadoc)
     * @see com.fastwire.carriere.service.RadService#getAlarmsByIP(java.lang.String, com.fastwire.carriere.user.vo.SessionVo)
     */
    @Override
    public List<RadAlarmVo> getAlarmsByIP(String ip, SessionVo sessionVo) throws ServiceException
    {
        List<RadAlarmVo> alms = null;
        try
        {
            if(!NetworkUtil.ipv4Check(ip))
            {
                CommonLogger.info(this, "IP is not correct: " + ip);
                return null;
            }
            RadProcessController ctrl = (RadProcessController) ResourceUtil
                    .getSessionBindClassByName(RadProcessControllerImp.class.getName(), sessionVo, false);
            
            alms = ctrl.getAlarmsByIP(ip);
        }
        catch (Exception e)
        {
            e.printStackTrace();

            String ex = ControllerProxy.getRootException(e);
            throw new ServiceException(ex);
        }

        return alms;
    }

    /* (non-Javadoc)
     * @see com.fastwire.carriere.service.RadService#getLogsByIP(java.lang.String, com.fastwire.carriere.user.vo.SessionVo)
     */
    @Override
    public List<RadLogVo> getLogsByIP(String ip, SessionVo sessionVo) throws ServiceException
    {
        List<RadLogVo> logs = null;
        
        try
        {
            if(!NetworkUtil.ipv4Check(ip))
            {
                CommonLogger.info(this, "IP is not correct: " + ip);
                return null;
            }
            
            RadProcessController ctrl = (RadProcessController) ResourceUtil
                    .getSessionBindClassByName(RadProcessControllerImp.class.getName(), sessionVo, false);
            
            logs = ctrl.getLogsByIP(ip);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            
            String ex = ControllerProxy.getRootException(e);
            throw new ServiceException(ex);
        }

        return logs;
    }

}

